<?php
namespace app\index\controller;

use app\common\controller\HomeBase;  // model
use think\Db;                       // db
use think\Session;
use think\Request;
use app\common\model\Qixi as QixiModel;
use app\common\model\Qixitxt;
use app\common\model\Category as CategoryModel;
use app\common\model\User as UserModel;

class Game extends HomeBase
{
    protected $_qixi_model;
    protected $_qixitxt_model;
    protected $category_model;
    protected $_user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->_qixi_model = new QixiModel();
        $this->_qixitxt_model = new Qixitxt();
        $this->category_model = new CategoryModel();
        $this->_user_model = new UserModel();
        $category_level_list  = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
//        dump($category_level_list);
    }

    public function index(){
//        dump(Session::get('user.openid'));
        return $this->fetch();
    }

    public function qixi(){
        $openid = Session::get('user.openid');

    }

    protected function getcategory(){
        $qixi_array = [];
        $qixi_cate = $this->category_model->where('pid',0)->select();
        $qixi_content = $this->category_model->where(['pid' => ['neq',0]])->select();
        foreach ($qixi_cate as $rss) {
            $con_list = [];
            foreach ($qixi_content as $q_con) {
                if($rss['id'] == $q_con['pid']){
                    array_push($con_list,$q_con['name']);
                }
            }
            array_push($qixi_array,$con_list);
        }
//        dump($qixi_array);
        return $qixi_array;
    }


    public function simg()
    {
        if(Request::instance()->has('name','get')){
            // 接收名字根据笔画返回对应的数据 保存名字到数据库
            $back_array = self::getcategory();
            //  rand(min,max)  ['qixi']   dump(Session::get('user.openid'));
            $sign = rand(0,sizeof($back_array));
            $qixi_str = implode(',',$back_array[$sign]);
            $data = [
                'username'=> Request::instance()->get('name'),
                'create_time' => date('Y-m-d H:i:s', time()),
                'qixi' => $qixi_str,
                'openid' => Session::get('user.openid')
            ];
            $result = $this->_qixi_model->save($data);
            return $this->thinkJson($back_array[$sign]);
        }else{
            $this->success('数据不能为空', 'Index/game');
        }
    }

    public function simg1()
    {
        if(Request::instance()->has('name','get')){
            // 接收名字根据笔画返回对应的数据 保存名字到数据库
           $back_array = [
            0 => ['艳遇','一夜情','酗酒','晴天','小雨'],
            1 => ['加班','加班','加班','加班','加班'],
            2 => ['加薪','加薪','加薪','加薪','加薪'],
            3 => ['闲逛','丢手机','坐警察局','看电视','虚度一天']
         ];
         $sign = rand(0,3);
         $qixi_str = implode(',',$back_array[$sign]);
         $data = [
            'username'=> Request::instance()->get('name'),
            'create_time' => date('Y-m-d H:i:s', time()),
            'qixi' => $qixi_str
        ];
        $result = $this->_qixi_model->save($data);
        return $this->thinkJson($back_array[$sign]);

        }else{
            $this->success('数据不能为空', 'Index/game');
        }
    }

    /*
     * 上传照片人脸识别
     * */
    public function face(){
        return $this->fetch();
    }

    /*
     * baidu 人脸识别测试接口
     * */
    public function cface(){
        if(Request::instance()->isPost()){
            dump(Request::instance()->post());
        }
//        $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
//        $imageType = 'URL';
//        // 调用人脸检测
//        $result = $this->client->detect($image, $imageType);
//        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    /*
     * baidu 人脸识别测试接口
     * */
    public function tface(){
        $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
        $imageType = 'URL';
        // 调用人脸检测
        $result = $this->client->detect($image, $imageType);
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }






}